//
//  cCLoginViewController.m
//  cyChat
//
//  Created by 楼楚 on 15/12/4.
//  Copyright © 2015年 Chu Lou. All rights reserved.
//

#import "cCLoginViewController.h"
#import "cCRegisterViewController.h"
#import "cCNavigationController.h"
@interface cCLoginViewController ()<cCRegisterViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *userLabel;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;


@end

@implementation cCLoginViewController

-(void)viewDidLoad
{
    [super viewDidLoad];
//    设置pwdField和loginBtn的样式
    self.pwdField.background = [UIImage stretchedImageWithName:@" operationbox_text"];
    
    [self.pwdField addLeftViewWithImage:@"Card_Lock" ];
    
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"fts_green_btn" ] forState:UIControlStateNormal];
    [self.loginBtn setBackgroundImage:[UIImage imageNamed:@"fts_green_btn_HL"] forState:UIControlStateHighlighted];
//    UIImageView *lockView = [[UIImageView alloc]init];
//    lockView.bounds = CGRectMake(0, 0,30, 30);
//    lockView.image = [UIImage imageNamed:@"Card_Lock"];
//    self.pwdField.leftViewMode = UITextFieldViewModeAlways;
//    self.pwdField.leftView = lockView;
    
    
    
//    设置用户名为上次登录的用户名
//    从单例中获取用户名
    
    NSString *user = [cCUserInfo sharedcCUserInfo].user;
    self.userLabel.text = user;
    
}
- (IBAction)loginBtnClick
{
    //    登录
    /*
     官方的登录实现
     1 把密码和用户名放在cCUserInfo的单例
     2 调用AppDelegate的一个login 连接服务并登录
     
     */
    
    
    cCUserInfo *userInfo = [cCUserInfo sharedcCUserInfo];
    
    userInfo.user = self.userLabel.text;
    
    userInfo.pwd = self.pwdField.text;
    [self login];
    
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//获取注册控制器
    id destVc = segue.destinationViewController;
    if ([destVc isKindOfClass:[cCNavigationController class]]) {cCNavigationController *nav = destVc;
//        获取根控制器
        if ([nav.topViewController isKindOfClass:[cCRegisterViewController class]]) {
           cCRegisterViewController *registerVc = (cCRegisterViewController *)nav.topViewController;
          //  设置注册控制器的代理
        registerVc.delegate =self;
        }
 }
    
    
}

#pragma mark - registerViewController的代理
- (void)registerViewControllerDidFinshRegister
{
    NSLog(@"完成注册");
//   完成注册  userlabel显示注册的用户名
    self.userLabel.text = [cCUserInfo sharedcCUserInfo].registerUser;
    
//    提示
//    [MBProgressHUD showMessage:@"请重新输入密码进行登录" toView:self.view];
    
}

@end
